package com.wwf;

import com.wwf.util.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;

/**
 * @author wwf
 * @date 2021年01月07日 11:00:24
 */
public class UserServerHandler extends ChannelInboundHandlerAdapter {

    private static ApplicationContext applicationContext;
   public UserServerHandler(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        /*String str = msg.toString();
        if(str.startsWith("UserService")){
            String result = new UserServiceImpl().sayHello(str.substring(str.lastIndexOf("#")+1));
            ctx.writeAndFlush(result);
        }*/
        System.out.println("读取客户端请求");
        RpcRequest rpcRequest = (RpcRequest)msg;
        Object bean = this.applicationContext.getBean(rpcRequest.getRequestid());
        Method method = bean.getClass().getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
        Object invoke = method.invoke(bean, rpcRequest.getParameters());
        ctx.writeAndFlush(invoke.toString());
    }
}
